﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DMC.Database;
using Microsoft.Reporting.WinForms;

namespace prjBaoCaoHoSoPhuongGuiLenQuan
{
    public partial class frmBaoCaoHoSoPhuongGuiLenQuan : Form
    {
        public clsDatabase cls;
        public DataTable dtDVHC;
        public ThongTinHoSo clsthongtin;
        public string strMaDVHC = "";
        public frmBaoCaoHoSoPhuongGuiLenQuan(string vconnect,string vMaDVHC)
        {
            cls = new clsDatabase();            
            cls.Connection = vconnect;
            cls.OpenConnection();
            strMaDVHC = vMaDVHC;
            dtDVHC = new DataTable();
            InitializeComponent();
            LoadcbbDVHC();
        }
        public void LoadcbbDVHC()
        {
            string[] para = { "@MaDVHC" };
            string[] values = { strMaDVHC };
            //clsdata.getValue(ref dtDVHC, "CT_SelectDonViHanhChinh", para, values);
            cls.getValue(ref dtDVHC, "CT_SelectDonViHanhChinh", para, values);
            if (dtDVHC.Rows.Count > 0)
            {
                cmbDonViHanhChinh.DataSource = dtDVHC;
            }
            else
            {
                cmbDonViHanhChinh.DataSource = null;
            }
            cmbDonViHanhChinh.DisplayMember = "Ten";
            cmbDonViHanhChinh.ValueMember = "MaDonViHanhChinh";
        }
        private void cmbDonViHanhChinh_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private void frmBaoCaoHoSoPhuongGuiLenQuan_Load(object sender, EventArgs e)
        {
            //this.reportViewer1.RefreshReport();          
        }
        private void LoadRptHoSo()
        {
            List<ThongTinHoSo> list = new List<ThongTinHoSo>();
            DataTable dt = new DataTable();
            string[] para = { };
            string[] values = { };
            cls.getValue(ref dt, "CT_SelectHoSoChuyenGiuaPhuongVaQuan", para, values);
            if (chkDonViHanhChinh.Checked == true && cmbDonViHanhChinh.DataSource != null) LocTheoDonViHanhChinh(ref dt);
            if (dtpTuNgay.Checked == true) LocTheoNgayGui(ref dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                clsthongtin = new ThongTinHoSo();
                clsthongtin.ToBanDo = dt.Rows[i]["ToBanDo"].ToString();
                clsthongtin.SoThua = dt.Rows[i]["SoThua"].ToString();
                clsthongtin.DiaChi = dt.Rows[i]["DiaChi"].ToString();
                clsthongtin.NguoiGui = dt.Rows[i]["NguoiGui"].ToString();
                clsthongtin.NgayGui = dt.Rows[i]["NgayGui"].ToString();
                clsthongtin.DienTichTuNhien = dt.Rows[i]["DienTichTuNhien"].ToString();
                clsthongtin.TenPhuong = dt.Rows[i]["TenPhuong"].ToString();
                list.Add(clsthongtin);
            }
            List<ReportParameter> param = new List<ReportParameter>();
            param.Add(new ReportParameter("TenQuan", LayTenQuan().ToUpper()));
            string tungay = "";
            string denngay = "";
            if (dtpTuNgay.Checked == true)
            {
                tungay = dtpTuNgay.Value.ToShortDateString();
                denngay = dtpDenNgay.Value.ToShortDateString();
            }
            param.Add(new ReportParameter("TuNgay", tungay));
            param.Add(new ReportParameter("DenNgay", denngay));

            rpvBaoCaoHoSoPhuongGuiQuan.LocalReport.SetParameters(param);

            this.rptBaoCaoHoSoPhuongGuiLenQuan.DataSource = list;
            this.rpvBaoCaoHoSoPhuongGuiQuan.RefreshReport();
        }
        private string LayTenQuan()
        {
            if (chkDonViHanhChinh.Checked == false ||(chkDonViHanhChinh.Checked==true && cmbDonViHanhChinh.DataSource==null))
            {
                DataTable dtdvhc = new DataTable();
                string[] para = { "@MaDVHC", "@flag" };
                string[] values = { strMaDVHC, "0" };
                cls.getValue(ref dtdvhc, "CT_SelectTenQuanByMaDonViHanhChinh", para, values);
                if (dtdvhc.Rows.Count > 0) return dtdvhc.Rows[0][0].ToString();
                else return "";
            }
            else if(chkDonViHanhChinh.Checked==true && cmbDonViHanhChinh.DataSource!=null)
            {
                DataTable dtdvhc = new DataTable();
                string[] para = { "@MaDVHC", "flag" };
                string[] values = { cmbDonViHanhChinh.SelectedValue.ToString(), "1" };
                cls.getValue(ref dtdvhc, "CT_SelectTenQuanByMaDonViHanhChinh", para, values);
                if (dtdvhc.Rows.Count > 0) return dtdvhc.Rows[0][0].ToString();
                else return "";
            }
            return "";
        }
        private void DinhDangRPV()
        {
            rpvBaoCaoHoSoPhuongGuiQuan.SetDisplayMode(DisplayMode.PrintLayout);
            rpvBaoCaoHoSoPhuongGuiQuan.ZoomMode = ZoomMode.Percent;
            rpvBaoCaoHoSoPhuongGuiQuan.ZoomPercent = 100;
        }
        private void LocTheoNgayGui(ref DataTable dt)
        {
            if (dtpTuNgay.Checked == true)
            {
                DataTable dttmp = new DataTable();
                DataRow[] drs = dt.Select("NgayGui >=#" + dtpTuNgay.Value + "# and NgayGui<= #" + dtpDenNgay.Value + "#");
                dttmp = dt.Clone();
                if (drs.Length > 0)
                {
                    foreach (DataRow dr in drs)
                    {
                        dttmp.ImportRow(dr);
                    }
                }
                dt = new DataTable();
                dt = dttmp;
            }
            //return dt;
        }
        private void LocTheoDonViHanhChinh(ref DataTable dt)
        {
            if (chkDonViHanhChinh.Checked == true && cmbDonViHanhChinh.DataSource!=null)
            {
                DataTable dttmp = new DataTable();
                DataRow[] drs = dt.Select("MaDonViHanhChinh='"+cmbDonViHanhChinh.SelectedValue.ToString()+"'");
                dttmp = dt.Clone();
                if (drs.Length > 0)
                {
                    foreach (DataRow dr in drs)
                    {
                        dttmp.ImportRow(dr);
                    }
                }
                dt = new DataTable();
                dt = dttmp;
            }
            //return dt;
        }
        private void btnBaoCaoThongKe_Click(object sender, EventArgs e)
        {
            try
            {
                LoadRptHoSo();
                DinhDangRPV();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void chkDonViHanhChinh_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDonViHanhChinh.Checked == true) cmbDonViHanhChinh.Enabled = true;
            else cmbDonViHanhChinh.Enabled = false;
        }

        private void dtpTuNgay_ValueChanged(object sender, EventArgs e)
        {
            if (dtpTuNgay.Checked == true) dtpDenNgay.Checked = true;
            else dtpDenNgay.Checked = false;
        }
    }
}
